﻿using Core.Utility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Language
{
    /// <summary>
    /// 带翻译的返回结果信息，通常用于方法返回bool型结果后，仍需要额外返回信息
    /// </summary>
    public class LanguageReturnResultInfo : LanguageReturnResultInfo<object>, IReturnResultInfo
    {

    }

    /// <summary>
    /// 带翻译的返回结果信息，通常用于方法返回bool型结果后，仍需要额外返回信息
    /// </summary>
    /// <typeparam name="TData"></typeparam>
    public class LanguageReturnResultInfo<TData>: ReturnResultInfo<TData>,IReturnResultInfo<TData>
    {
        private const string defaultMessage = $"操作失败";

        /// <summary>
        /// 默认构造
        /// </summary>
        /// <param name="successed">是否成功，默认值为false</param>
        /// <param name="message">
        /// 友好提示信息，通常用于日志输出，当值为 string.Empty 或 null 时，
        /// <para>
        /// 设置值为：<see cref="defaultMessage"/> 
        /// 并调用 <see cref="LanguageResource.GetResourceValue(string?, System.Globalization.CultureInfo?, bool)()"/> 进行翻译
        /// </para>
        /// </param>
        /// <param name="info">完整的提示信息，通常用于日志输出，当值为 string.Empty 或 null 时，
        /// <para>
        /// 设置值为：<see cref="defaultMessage"/> 
        /// 并调用 <see cref="LanguageResource.GetResourceValue(string?, System.Globalization.CultureInfo?, bool)()"/> 进行翻译
        /// </para></param>
        public LanguageReturnResultInfo(bool successed = false, string? message = null, string? info = null)
        {
            this.Successed = successed;
            if(string.IsNullOrEmpty(message))
            {
                message = LanguageResource.GetResourceValue(defaultMessage);
            }
            if (string.IsNullOrEmpty(info))
            {
                info = LanguageResource.GetResourceValue(defaultMessage);
            }
            this.Message = message;
            this.Info = info;
            if (string.IsNullOrEmpty(info))
            {
                this.Info = message;
            }
        }


        /// <summary>
        /// 友好提示信息，通常用于直接呈现给用户的提示信息
        /// <para>
        /// 默认值为：<see cref="defaultMessage"/> 
        /// 并调用 <see cref="LanguageResource.GetResourceValue(string?, System.Globalization.CultureInfo?, bool)()"/> 进行翻译
        /// </para>
        /// </summary>    
        /// <example>接口调用成功</example>
        [JsonProperty("message", Order = 1)]
        public override string? Message { get; set; } = LanguageResource.GetResourceValue(defaultMessage);

        private string? info;
        /// <summary>
        /// 执行返回的信息，非友好提示，将异常信息等处理至此；若未指定 <see cref="Info"/> 的值，将返回 <see cref="Message"/> 的值
        /// </summary>
        /// <example>操作成功</example>
        [JsonProperty("info", Order = 2)]
        public override string? Info
        {
            get
            {
                if (string.IsNullOrEmpty(info))
                {
                    return Message;
                }
                else
                {
                    return info;
                }
            }
            set => info = value;
        }
    }
}
